﻿using AutoMapper;
using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDomain.IServices;
using CozeDto.SysDicDtos;
using CozeDto.SysDicTypeDtos;
using D.UtilCore.Dtos;
using D.UtilFreesqlOrm;


namespace CozeDomain.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class SysDicService : BaseService<SysDicEntity>, ISysDicService
    {
        private readonly IMapper _mapper;
        private readonly ISysDicEntityRepository _repository;
        /// <summary>
        /// 构造注入
        /// </summary>
        public SysDicService(IMapper mapper, ISysDicEntityRepository repository)
        {
            // 用于base方法
            base._baseDal = repository;
            this._repository = repository;
            _mapper = mapper; 
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddSysDicAsync(ReqAddSysDic reqModel)
        {
            // 检查名称重复
            var exists = await _repository.ExistsAsync(m => m.Name == reqModel.Name);
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "该信息已存在" };
            }

            var insertEntity = _mapper.Map<ReqAddSysDic, SysDicEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = D.UtilFreesqlOrm.FreesqlOrm.NewId;

            var dbres = await _repository.AddAsync(insertEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteSysDicAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateSysDicAsync(ReqUpdateSysDic reqModel)
        {
            var updateEntity = await _repository.GetModelAsync(m => m.Id == reqModel.Id);
            if (updateEntity == null)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "该信息不存在" };
            }

            updateEntity.Val = reqModel.Val;
            updateEntity.Description = reqModel.Description;
            updateEntity.ParentId = reqModel.ParentId;
            updateEntity.Name = reqModel.Name;
            updateEntity.Status = reqModel.Status;
            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUser = reqModel.UpdateUser;

            var dbres = await _repository.UpdateAsync(updateEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<RepSysDicInfo>> GetSysDicAsync(string id)
        {
            var dbres = await _repository.GetModelAsync(m => m.Id == id);
            var res = _mapper.Map<SysDicEntity, RepSysDicInfo>(dbres);
            return res.GetResponse();
        }

        #region 获取分页列表

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageQuery"></param>
        /// <param name="async"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepQuerySysDicTreeTable>>> GetPagesAsync(ReqQuerySysDicPage pageQuery)
        {

            var dbres = await _repository.GetPagesAsync(pageQuery);

            var res = new PageModel<RepQuerySysDicTreeTable>()
            {
                PageNo = pageQuery.PageNo,
                PageSize = pageQuery.PageSize,
                Data = dbres.Item2,
                TotalCount = dbres.Item1,
                TotalPage = pageQuery.PageSize > 0 ? (int)Math.Ceiling(dbres.Item1 / (double)pageQuery.PageSize) : 0
            };
            return res.GetResponse();
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dicTypeUniqueCode"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepSelect>>> QueryDicSelect(string dicTypeUniqueCode)
        {
            var res = await _repository.QueryDicSelect(dicTypeUniqueCode);
            return res.GetResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dicTypeUniqueCode"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepTreeSelect>>> QueryDicTreeSelect(string dicTypeUniqueCode)
        {
            var res = await _repository.QueryDicTreeSelect(dicTypeUniqueCode);
            return res.GetResponse();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dicTypeUniqueCode"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepTreeList>>> QueryDicTreeSelect2(string dicTypeUniqueCode)
        {
            var res = await _repository.QueryDicTreeSelect2(dicTypeUniqueCode);
            return res.GetResponse();
        }
    }
}
